#ifndef _DevicesSPI_H_
#define _DevicesSPI_H_


#define SPI0_NSS_SET_HIGH()     GPIO_BOP(GPIOB) = GPIO_PIN_10
#define SPI0_NSS_SET_LOW()      GPIO_BC(GPIOB) = GPIO_PIN_10

#define SPI1_NSS_SET_HIGH()     GPIO_BOP(GPIOB) = GPIO_PIN_12
#define SPI1_NSS_SET_LOW()      GPIO_BC(GPIOB) = GPIO_PIN_12

#define SPI0_SPEED_HIGH()       do{ SPI_CTL0(SPI0) &= ~SPI_CTL0_PSC; SPI_CTL0(SPI0) |= SPI_PSC_4;   }while(0)
#define SPI0_SPEED_LOW()        do{ SPI_CTL0(SPI0) &= ~SPI_CTL0_PSC; SPI_CTL0(SPI0) |= SPI_PSC_256; }while(0)

#define SPI1_SPEED_HIGH()       do{ SPI_CTL0(SPI1) &= ~SPI_CTL0_PSC; SPI_CTL0(SPI1) |= SPI_PSC_4;   }while(0)
#define SPI1_SPEED_LOW()        do{ SPI_CTL0(SPI1) &= ~SPI_CTL0_PSC; SPI_CTL0(SPI1) |= SPI_PSC_256; }while(0)


void vSPI0Init(void);
void vSPI1Init(void);

#define ucSPI0WriteReadByte(ucByte)                     ucSPIWriteReadByte(SPI0, (ucByte))
#define cSPI0WriteDatas(pBuffer, iLength)               cSPIWriteDatas(SPI0, (pBuffer), (iLength))
#define cSPI0ReadDatas(pBuffer, iLength)                cSPIReadDatas(SPI0, (pBuffer), (iLength))
#define cSPI0DmaWriteDatas(pBuffer, iLength, iWaitUs)   cSPIDmaWriteDatas(SPI0, (pBuffer), (iLength), iWaitUs)

#define ucSPI1WriteReadByte(ucByte)                     ucSPIWriteReadByte(SPI1, (ucByte))
#define cSPI1WriteDatas(pBuffer, iLength)               cSPIWriteDatas(SPI1, (pBuffer), (iLength))
#define cSPI1ReadDatas(pBuffer, iLength)                cSPIReadDatas(SPI1, (pBuffer), (iLength))
#define cSPI1DmaWriteDatas(pBuffer, iLength, iWaitUs)   cSPIDmaWriteDatas(SPI1, (pBuffer), (iLength), iWaitUs)

#define ucSPI2WriteReadByte(ucByte)                     ucSPIWriteReadByte(SPI2, (ucByte))
#define cSPI2WriteDatas(pBuffer, iLength)               cSPIWriteDatas(SPI2, (pBuffer), (iLength))
#define cSPI2ReadDatas(pBuffer, iLength)                cSPIReadDatas(SPI2, (pBuffer), (iLength))
#define cSPI2DmaWriteDatas(pBuffer, iLength, iWaitUs)   cSPIDmaWriteDatas(SPI2, (pBuffer), (iLength), iWaitUs)

uint8_t ucSPIWriteReadByte(uint32_t uiSpiPeriph, uint8_t ucByte);
int8_t cSPIWriteDatas(uint32_t uiSpiPeriph, void *pvBuff, int32_t iLength);
int8_t cSPIReadDatas(uint32_t uiSpiPeriph, void *pvBuff, int32_t iLength);
int8_t cSPIDmaWriteDatas(uint32_t uiSPIxPeriph, void *pvDatas, int32_t iLength, int32_t iWaitUs);

int8_t cSPITransStatus(uint32_t uiSpiPeriph);


#endif
